<?php
namespace Manage\Controller;
use Common\Controller\ManageBaseController;
use Think\Model;
use Common\Model\ArticleModel;
use Common\Model\JiluModel;
use Common\Model\MemberModel;

class JiluController extends ManageBaseController{
	
	/*
	 * 商家充值记录
	 */
	public function lists($ajax=null,$status=null,$page=null,$rows=null) {
		$map = array(); $status_view = array('default'=>'所有','del'=>'已删除','forbid'=>'禁用','allow'=>'正常');
		//查询条件 处理
		$now_status = $status_view['default'];
		if (isset($status) && key_exists($status, JiluModel::$mystat)) { //指定查询状态
			$map['status'] = JiluModel::$mystat[$status];
			$now_status = $status_view[$status];
		}else {
			$map['status'] = array('eq',1); //默认查询状态为未删除的数据
		}
		if ($page <= 0) {	$page = 1;	}
		if ($rows > 0) {	$listRows = $rows;
		}else {	$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;	}
		$map['cate'] = 1;
		
		$member = new MemberModel();
		
		//筛选条件
		$filter = I('param.');
		if ($filter['keyword']) {
			$map['sid'] = $member->where("account = '".$filter['keyword']."'")->getField("id");
			$this->assign('keyword', $filter['keyword']);
		}
		
		/******************/
		if ($ajax==='y') {
			$model = new Model('Jilu');
			$total = $model->where($map)->count(); //总记录数
			$list = $model->where($map)->order('createtime desc')->page("$page,$listRows")->select();
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']); 
				$v['account'] = $member->where("id = ".$v['sid'])->getField("account");
			}
			unset($v);		
			$list = array("total"=>(int)$total,"rows"=>$list);
			$this->ajaxReturn($list);
		}else {
			$this->assign('_page', $page);
			$this->assign('_rows', $listRows);
			$this->assign('now_status',$now_status); //当前页面筛选的状态
			
			// 记录当前列表页的cookie
			cookie(C('CURRENT_URL_NAME'),$_SERVER['REQUEST_URI']);
			$this->display();		
		}
	}

	/*
	 * 账户充值
	 * 1、账户充值
	 * 2、添加充值记录
	 */
	public function insert($mid) {
		$return = array('status'=>0,'info'=>'');	

		$model = new Model();
		$jilu = new JiluModel();
		$member = new MemberModel();
		
		$model->startTrans();//开启 事物
		
		if( false === $member->where("id = ".$mid)->setInc("total_money",I('post.money')) || false === $member->where("id = ".$mid)->setInc("able_money",I('post.money'))){//修改余额
			$model->rollback();
			$return['info'] = $member->getError();
		}
		$data = array();
		$data['sid'] = $mid;
		$data['money'] = I('post.money');
		$data['remark'] = I('post.remark');
		$data['createtime'] = time();		
		$data['cate'] = 1;
		$data['status'] = 1;
		$data['yue'] = $member->where("id = ".$mid)->getField("total_money");	
		if (false === $jilu->add($data)) {//添加充值记录
			$return['info'] = $jilu->getError();
		}
		
		$model->commit();
		$return['status'] = 1;
		$return['info'] = '添加成功';
		
		$this->ajaxReturn($return);
	}

}